﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class BtnPermissionController : Controller
    {
        //
        // GET: /BtnPermission/

        public ActionResult BtnPermissionList()
        {
            return View();
        }
        public JsonResult List()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "Parent", Operator = "!=", Value = "0" });
            Expression<Func<View_BD_btnPermission, bool>> exp = SearchExpression.FindByGroup<View_BD_btnPermission>(listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.View_BD_btnPermissionBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, o => o.Id, true);
            count = BLLSession.Current.View_BD_btnPermissionBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        /// <summary>
        /// 新增按钮权限
        /// </summary>
        public string AddBtnPermission()
        {

            BD_btnPermission per = new BD_btnPermission();
            ModelTools.Form2EditModel(Request.Form, per);
            per.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            BLLSession.Current.BD_btnPermissionBLL.Add(per);
            return JsonHelper.toDefaultMsg(true, "保存成功");
        }
        /// <summary>
        /// 修改按钮权限
        /// </summary>
        public string EditBtnPermission()
        {
            int Id = Commons.String2Int32(Request.Form["Id"]);
            BD_btnPermission perNew = DALSession.Current.BD_btnPermissionDAL.GetListBy(d => d.Id == Id).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            perNew.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            BLLSession.Current.BD_btnPermissionBLL.ModifyAllModel(perNew);
            return JsonHelper.toDefaultMsg(true, "保存成功");
        }

        /// <summary>
        /// 删除按钮权限
        /// </summary>
        public string DelBtnPermission()
        {
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            BLLSession.Current.BD_btnPermissionBLL.DelBy(p => p.Id == Id);
            BLLSession.Current.BD_RoleBtnPermissionBLL.DelBy(r => r.BId == Id);
            BLLSession.Current.BD_UserVipBtnPermissionBLL.DelBy(u => u.vipBtnPermission == Id);
            return JsonHelper.toDefaultMsg(true, "删除成功");
        }
    }
}
